import 'package:flutter/material.dart';

class FFCalculateText {
 ///计算文本的行数
  static int calculateTextLines(String text, TextStyle style, double maxWidth) {
    final textSpan = TextSpan(text: text, style: style);
    final textPainter = TextPainter(
      text: textSpan,
      textDirection: TextDirection.ltr,
      maxLines: null,
    );
    textPainter.layout(maxWidth: maxWidth);
    return textPainter.computeLineMetrics().length;
  }

  ///计算文本的尺寸，一行展示
  static Size calculateCharSize(String char, TextStyle style) {
    final TextPainter textPainter = TextPainter(
      text: TextSpan(text: char, style: style),
      textDirection: TextDirection.ltr,
      maxLines: 1,
    )..layout(minWidth: 0, maxWidth: double.infinity);

    return textPainter.size;
  }

  ///计算文本的高度，多行展示
  static double calculateTextHeight(String char,double width, TextStyle style) {
    final TextPainter textPainter = TextPainter(
      text: TextSpan(text: char, style: style),
      textDirection: TextDirection.ltr,
      maxLines: 9999999,
    )..layout(maxWidth: width);

    return textPainter.height;
  }
}
